package cn.edu.xmu.javaee.productdemo.service;


import cn.edu.xmu.javaee.productdemo.dao.bo.Product;
import cn.edu.xmu.javaee.productdemo.dao.ProductDao;
import cn.edu.xmu.javaee.productdemo.service.vo.ProductVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@RequiredArgsConstructor
@Service
public class ProductService {

    private final ProductDao productDao;

    public ProductVo createDraft(Product product){
        Product draft =  this.productDao.insert(product);
        ProductVo draftVo = ProductVo.builder().id(draft.getId()).name(draft.getName()).originalPrice(draft.getOriginalPrice()).originPlace(draft.getOriginPlace()).build();
        return draftVo;
    }

    public ProductVo getDraft(Long shopId, Long draftId){
        Product draft = this.productDao.findById(shopId, draftId);
        log.debug("getDraft: draft = {}", draft);
        ProductVo draftVo = ProductVo.builder().id(draft.getId()).name(draft.getName()).originalPrice(draft.getOriginalPrice()).originPlace(draft.getOriginPlace()).build();
        return draftVo;
    }

    public void delDraft(Long shopId, Long draftId){
        this.productDao.findById(shopId, draftId);
        this.productDao.deleteById(draftId);
    }

    public void updateDraft(Long shopId, Product draft){
        this.productDao.findById(shopId, draft.getId());
        this.productDao.updateById(draft);
    }
}
